<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>购物车</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/common.css" />
    <link rel="stylesheet" type="text/css" href="../../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../../css/aui-pull-refresh.css" />
    <link rel="stylesheet" type="text/css" href="../../css/swiper.min.css" />
    <link rel="stylesheet" type="text/css" href="../../css/style.css" />
    <style>
        #app {
            float: left;
            background: #f5f5f5;
        }

        .aui-bar.aui-bar-light.share_nav {
            background: #fff;
            padding-top: 2.5rem;
            position: fixed;
            margin-top: 0 !important;
        }

        .share_nav .share_nav_title {
            height: 4.4rem;
            line-height: 4.4rem;
            position: relative;
            top: 0;
            left: 0;
            right: 0;
            font-size: 1.6rem;
            color: #000;
        }

        .share_nav .share_nav_left {
            top: 2.5rem;
            left: 1.3rem;
            position: absolute;
        }

        .share_nav .share_nav_right {
            top: 2.5rem;
            right: 1.3rem;
            position: absolute;
        }

        .share_nav .share_nav_right span {
            position: absolute;
            top: 0.8rem;
            right: 0;
            width: 1.2rem;
            height: 1.2rem;
            line-height: 1.2rem;
            color: #fff;
            background: #ed6a20;
            font-size: 1rem;
            border-radius: 100%;
        }

        .share_nav img {
            height: 1.7rem;
            margin-top: 1.05rem;
        }

        .dingwei {
            width: 1rem;
            display: inline-block;
            margin-top: 0rem!important;
            position: relative;
            top: 0.35rem;
            left: 1rem;
            height: 1.55rem;
        }

        .administration {
            float: right;
            color: #999;
            font-size: 1.4rem;
            margin: 1.15rem 1.1rem 0 0;
            float: right;
        }

        .content {
            width: 100%;
            box-sizing: border-box;
            margin: 6.9rem 0 0 0;
            float: left;
        }

        .content_shop {
            width: 100%;
            float: left;
            box-sizing: border-box;
            /*padding: 0 1.25rem 0 1.3rem;*/
        }

        .content_shop_title {
            width: 100%;
            height: 5.05rem;
            float: left;
            box-sizing: border-box;
            padding: 0 1.43rem;
        }

        .content_shop_title_input {
            width: 2.45rem;
            height: 100%;
            float: left;
        }

        .content_shop_title_text {
            width: 31.9rem;
            height: 100%;
            float: left;
            line-height: 5.05rem;
            font-size: 1.4rem;
            color: #333333;
            font-weight: 600;
        }

        .month_shop_checkbox {
            margin: 1.75rem 0 0 0;
        }

        .aui-checkbox {
            border-radius: 100%;
            width: 1.75rem;
            height: 1.75rem;
            border: solid 0.1rem #999ba4;
        }

        .aui-checkbox:checked {
            background: #fc3131;
            border: solid 1px #fc3131;
        }

        .aui-checkbox::after {
            width: 1rem!important;
            height: 0.3rem!important;
            margin-left: -0.5rem!important;
        }

        .aui-checkbox::before {
            width: 1rem!important;
            height: 0.3rem!important;
            margin-left: -0.5rem!important;
        }

        .content_shop_title_text .home {
            width: 1.65rem;
            height: 1.55rem;
            display: inline-block;
            margin: 0 0.85rem 0 0;
            position: relative;
            top: 0.4rem;
        }

        .content_shop_title_text .arrow {
            width: 0.55rem;
            height: 1.1rem;
            display: inline-block;
            margin: 0 0 0 1rem;
            position: relative;
            top: 0.1rem;
        }

        .content_shop_commodity {
            width: 100%;
            /*height: 11.9rem;*/
            float: left;
            margin: 0 0 1.3rem 0;
            position: relative;
            -webkit-transition: all 0.2s;
            transition: all 0.2s;
            display: flex;
        }

        .content_shop_commodity_ul {
            width: 100%;
            overflow: hidden;
        }

        .content_shop_commodity[data-type="0"] {
            transform: translate3d(0, 0, 0);
        }

        .content_shop_commodity[data-type="1"] {
            transform: translate3d(-12rem, 0, 0);
        }

        .content_shop_commodity_box {
            width: 100%;
            height: 11.9rem;
            /*background: #fff;*/
        }

        .delete {
            width: 6rem;
            height: 11.9rem;
            background-color: #ed6a20;
            box-shadow: 0rem 0.25rem 0.95rem 0.05rem rgba(212, 211, 211, 0.5);
            text-align: center;
            line-height: 11.9rem;
            color: #ffffff;
            font-size: 1.2rem;
            position: absolute;
            top: 0;
            right: -6rem;
        }

        .extension {
            width: 6rem;
            height: 11.9rem;
            background-color: #f01818;
            box-shadow: 0rem 0.25rem 0.95rem 0.05rem rgba(212, 211, 211, 0.5);
            text-align: center;
            line-height: 11.9rem;
            color: #ffffff;
            font-size: 1.2rem;
            position: absolute;
            top: 0;
            right: -12rem;
        }

        .content_shop_commodity_input {
            width: 2.45rem;
            height: 100%;
            float: left;
            margin: 0 0 0 1.3rem;
        }

        .content_shop_commodity_input .month_shop_checkbox {
            margin: 5.05rem 0 0 0;
        }

        .content_shop_commodity_content {
            width: 32.5rem;
            height: 11.9rem;
            background-color: #ffffff;
            box-shadow: 0rem 0.25rem 0.95rem 0.05rem rgba(212, 211, 211, 0.5);
            border-radius: 0.25rem;
            float: left;
            position: relative;
        }

        .content_shop_commodity_img {
            width: 9rem;
            height: 9rem;
            border: solid 0.05rem #ececec;
            float: left;
            margin: 1.45rem 0 0 1.2rem;
        }

        .bottom {
            width: 37.5rem;
            height: 4.9rem;
            background-color: #ffffff;
            box-shadow: 0rem -0.05rem 0.15rem 0rem rgba(0, 0, 0, 0.19);
            position: fixed;
            bottom: 0rem;
            z-index: 10;
        }

        .content_shop_commodity_title {
            width: 22.2rem;
            font-size: 1.3rem;
            line-height: 1.5rem;
            letter-spacing: 0.02rem;
            color: #333333;
            float: left;
            margin: 1.6rem 0 0 0;
            overflow: hidden;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            padding: 0 0 0 0.6rem;
            box-sizing: border-box;
        }

        .content_shop_commodity_type {
            width: 22.2rem;
            float: left;
            font-size: 1.2rem;
            color: #999999;
            margin: 0.55rem 0 2.8rem 0;
            padding: 0 0 0 0.6rem;
            box-sizing: border-box;
            height: 1.2rem;
            line-height: 1.2rem;
        }

        .content_shop_commodity_price {
            width: 22.2rem;
            float: left;
            font-size: 1.7rem;
            color: #f01818;
            line-height: 1.7rem;
            padding: 0 0 0 0.6rem;
            box-sizing: border-box;
        }

        .content_shop_commodity_price span {
            font-size: 1.3rem;
            color: #f01818;
        }

        .content_shop_commodity_numBox {
            width: 7.6rem;
            position: absolute;
            right: 0;
            bottom: 1.45rem;
            height: 2.1rem;
            right: 0.5rem;
        }

        .numBox_reduce {
            width: 2rem;
            height: 2.1rem;
            float: left;
            background: url(../../image/cart/icon5.png);
            background-size: 100% 100%;
        }

        .numBox_reduce_active {
            width: 2rem;
            height: 2.1rem;
            float: left;
            background: url(../../image/cart/icon7.png);
            background-size: 100% 100%;
        }

        .numBox_add {
            width: 2rem;
            height: 2.1rem;
            float: left;
            background: url(../../image/cart/icon6.png);
            background-size: 100% 100%;
        }

        .numBox_num {
            width: 3.6rem;
            height: 2.1rem;
            line-height: 2.1rem;
            font-size: 1.3rem;
            float: left;
            background-color: #f6f6f6;
            text-align: center;
            color: #333333;
            font-weight: 600;
        }

        .delete_bottom_block {
            width: 100%;
            height: 4.9rem;
            float: left;
        }

        .show {
            display: block!important;
        }

        .bottom_left {
            width: 26rem;
            height: 100%;
            float: left;
            line-height: 4.9rem;
            height: 4.9rem;
            font-size: 1.4rem;
        }

        .all_checkbox {
            margin: 1.55rem 0.6rem 0 1.3rem;
            float: left;
        }

        .bottom_left .price_title {
            font-size: 1.7rem;
            margin-left: 2.35rem;
            font-weight: 600;
        }

        .bottom_left .price {
            font-size: 1.7rem;
            letter-spacing: 0.04rem;
            color: #f01818;
            font-weight: 600;
        }

        .bottom_right {
            width: 10.7rem;
            height: 4.9rem;
            background-image: linear-gradient(-90deg, #ff4e18 0%, #f9280d 52%, #f20101 100%), linear-gradient( #ff8600, #ff8600);
            background-blend-mode: normal, normal;
            float: right;
            color: #ffffff;
            font-size: 1.4rem;
            font-weight: 600;
            text-align: center;
            line-height: 4.9rem;
        }
    </style>
</head>

<body>
    <div id="app">
        <header class="aui-bar aui-bar-nav aui-bar-light share_nav">
            <a class="aui-pull-left share_nav_left" onclick="close_win()">
                <img src="../../image/share/share_index_nav4.png" alt="">
            </a>
            <div class="aui-title share_nav_title">购物车<img src="../../image/cart/icon1.png" alt="" class="dingwei"></div>
            <a class="aui-pull-right share_nav_right">
                <div class="administration" @click="admin()">管理</div>
                <div class="cartMessage" @click="goMsg">
                    <img src="../../image/share/share_index_nav3.png" alt="">
                    <span>5</span>
                </div>
            </a>
        </header>
        <div class="content aui-refresh-content">
            <div class="content_shop" v-for="(items,key,index) in shop_list" :key="index">
                <div class="content_shop_title">
                    <div class="content_shop_title_input">
                        <input type="checkbox" :id="items.store_id" v-model="checkShop" :value="items.store_id" class="aui-checkbox month_shop_checkbox" @click.stop="check_shop($event,key)">
                    </div>
                    <div class="content_shop_title_text">
                        <img :src="items.store_logo" class="home" alt="">{{ items.store_name }}
                        <img src="../../image/cart/icon4.png" class="arrow" alt="">
                    </div>
                </div>
                <div class="content_shop_commodity_ul">
                    <div class="content_shop_commodity" data-type="0" v-for="(item,k,index) in items.goods_list" :key="index">
                        <div class="content_shop_commodity_box" @click="skip" @touchstart.capture="touchStart" @touchend.capture="touchEnd">
                            <div class="content_shop_commodity_input">
                                <input type="checkbox" :id="item.cart_id" v-model="checkCommodity" :value="item.cart_id" class="aui-checkbox month_shop_checkbox" name="" value="" @click.stop="check_commodity($event,k,key)">
                            </div>
                            <div class="content_shop_commodity_content">
                                <div class="content_shop_commodity_img" :style="{backgroundImage:'url(' + item.goods_logo + ')', backgroundRepeat:'no-repeat', backgroundPosition:'center center', backgroundSize: 'contain'}"></div>
                                <div class="content_shop_commodity_title">
                                    {{item.goods_name}}
                                </div>
                                <div class="content_shop_commodity_type">
                                    {{ item.goods_skuinfo }}
                                </div>
                                <div class="content_shop_commodity_price">
                                    <!-- <span>￥</span>{{ item.goods_price }} -->
                                    <span>￥</span>{{ item.goods_price }}
                                </div>
                                <div class="content_shop_commodity_numBox">
                                    <div :class="[ item.goods_nums > 1 ? 'numBox_reduce_active' : 'numBox_reduce' ]" @click="numBox_reduce(item.cart_id)"></div>
                                    <div class="numBox_num">{{item.goods_nums}}</div>
                                    <div class="numBox_add" @click="numBox_add(item.cart_id)"></div>
                                </div>
                            </div>
                        </div>
                        <div class="delete" @click="deleteItem(item.cart_id)">
                            删除
                        </div>
                        <div class="extension">
                            推广
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="delete_bottom_block">

        </div>
        <div class="bottom">
            <div class="bottom_left">
                <input type="checkbox" name="" value="" class="aui-checkbox all_checkbox" @click.stop="all_select($event)">全选
                <span class="price_title">合计：</span><span class="price">￥{{total_money}}</span>
            </div>
            <div class="bottom_right" @Click="goOrder()">
                去结算({{total_num}})
            </div>
        </div>
    </div>
</body>

</html>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/zepto.js"></script>
<script type="text/javascript" src="../../script/vue.min.js"></script>
<script type="text/javascript" src="../../script/swiper.min.js"></script>
<script type="text/javascript" src="../../script/utils.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/aui-tab.js"></script>
<script type="text/javascript" src="../../script/aui-scroll.js"></script>
<script type="text/javascript" src="../../script/aui-pull-refresh.js"></script>
<script type="text/javascript" src="../../script/axios.min.js"></script>
<script type="text/javascript" src="../../script/axios-config.js"></script>

<script type="text/javascript">
    apiready = function() {
            // setRefresh()
            var token = $api.getStorage('token');
            if (!token) {
                api.openWin({
                    name: 'orderTrue',
                    url: '../user/login/login.html',
                    pageParam: {
                        test: ''
                    }
                });
            }
        }
        //刷新
    function setRefresh() {
        var pullRefresh = new auiPullToRefresh({
            container: document.querySelector('.aui-refresh-content'),
            triggerDistance: 100
        }, function(ret) {

            if (ret.status == "success") {
                setTimeout(function() {
                    pullRefresh.cancelLoading(); //刷新成功后调用此方法隐藏
                }, 1500)
            }
        })
    }

    function closeYM() {
        api.closeWin({});
    }

    var vue = new Vue({
        el: '#app',
        data: {
            shop_list: [],
            startX: 0,
            endX: 0,
            checkShop: [],
            checkCommodity: [],
            administration: false,
            total_money: "0.00",
            total_num: 0,
            cartids: ''
        },
        mounted() {},
        created: function() {
            this.getCartList();
        },
        computed: {
            // cartAllPrice: function() {
            //   console.log(JSON.stringify(this.shop_list))
            //     return this.shop_list.reduce((total, item) => this.total + (this.checkCommodity.indexOf(item.goods_list.cart_id) !== -1 ? Number(item.goods_list.goods_allprice) : 0),0)
            // }
        },
        methods: {
            //获取列表
            getCartList: function() {
                var $_this = this;
                const data = {
                    token: $api.getStorage('token'),
                }
                Axios.post(window.Url.cart_list, data).then(function(res) {
                    if (res.status == 1) {
                        $_this.shop_list = res.cart_list_1;
                    } else {
                        $_this.shop_list = [];
                        api.toast({
                            msg: res.info,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }).catch((err) => {
                    $_this.shop_list = []
                    api.alert({
                        msg: JSON.stringify(err.info)
                    });
                })
            },
            //选择
            check_commodity: function(e, k, key) {
                var check = e.target.checked
                var checkCommodity = this.checkCommodity
                var shop_list = this.shop_list //获取数组列表
                var shop_commodity_num = shop_list[key].goods_list.length;
                var goods_check_length = 0
                var f_id = shop_list[key].store_id
                for (var a = 0; a < shop_commodity_num; a++) { //获取选中长度
                    var id = shop_list[key].goods_list[a].cart_id
                    if (checkCommodity.indexOf(id) > -1) {
                        goods_check_length++
                    }
                }

                if (shop_commodity_num == goods_check_length) { //如果商品菜单与选中的长度相等相等
                    if (this.checkShop.indexOf(f_id) == -1) {
                        this.checkShop.push(f_id);
                    }
                } else {
                    var checkShopNum = this.checkShop.indexOf(f_id);
                    if (checkShopNum > -1) {
                        this.checkShop.splice(checkShopNum, 1)
                    }
                }
                this.getAllPice();
                this.getAllPice();
            },
            // 全选
            all_select: function(e) {
                var check = e.target.checked //是否选中
                if (check == true) {
                    for (var a = 0; a < this.shop_list.length; a++) { //循环
                        let store_id = this.shop_list[a].store_id;
                        this.checkShop.push(store_id);
                        for (var b = 0; b < this.shop_list[a].goods_list.length; b++) {
                            let cart_id = this.shop_list[a].goods_list[b].cart_id;
                            this.checkCommodity.push(cart_id);
                        }
                    }

                } else {
                    this.checkCommodity = [];
                    this.checkShop = [];
                }
                this.getAllPice();
            },
            //管理
            admin: function() {
                this.administration == true ? this.administration = false : this.administration = true;
            },
            //选择商店下全部商品
            check_shop: function(e, key) {
                var check = e.target.checked //是否选中
                var key = key //获取key
                var index = e.target._value

                if (check == true) {
                    for (var a = 0; a < this.shop_list.length; a++) {
                        if (this.shop_list[a].store_id == index) {
                            for (var b = 0; b < this.shop_list[a].goods_list.length; b++) {
                                var id = this.shop_list[a].goods_list[b].cart_id;
                                if (this.checkCommodity.indexOf(id) == -1) {

                                    this.checkCommodity.push(id);
                                }
                            }
                        }
                    }
                    if (this.checkShop.indexOf(index) == -1) {
                        this.checkShop.push(index)
                    }
                } else {
                    for (var a = 0; a < this.shop_list.length; a++) {
                        if (this.shop_list[a].store_id == index) {
                          // this.checkShop.splice(i, 1)
                            for (var b = 0; b < this.shop_list[a].goods_list.length; b++) {
                                var id = this.shop_list[a].goods_list[b].cart_id
                                var shop_id = this.checkCommodity.indexOf(id)
                                this.checkCommodity.splice(shop_id, 1)
                            }
                        }
                    }

                }
                this.getAllPice();
            },
            //减少数量
            numBox_reduce: function(cart_id) {

                var $_this = this;
                let shopNum = 0;

                for (let x = 0; x < $_this.shop_list.length; x++) {
                    for (let y = 0; y < $_this.shop_list[x].goods_list.length; y++) {
                        if ($_this.shop_list[x].goods_list[y].cart_id == cart_id) {
                            if ($_this.shop_list[x].goods_list[y].goods_nums <= 1) {
                                api.toast({
                                    msg: '亲，该宝贝不能减少了哟',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                                return;
                            } else {
                                shopNum = --$_this.shop_list[x].goods_list[y].goods_nums;
                            }
                        }
                    }
                }
                $_this.getAllPice();
                const data = {
                    token: $api.getStorage('token'),
                    cart_id: cart_id,
                    nums: shopNum
                }
                $_this.changeCartGoodsNums(data);
            },
            //改变购物车数量
            changeCartGoodsNums: function(data) {
                Axios.post(window.Url.change_cart_goods_nums, data).then(function(res) {
                    if (res.status == 1) {
                        api.toast({
                            msg: res.info,
                            duration: 2000,
                            location: 'middle'
                        });
                    } else {
                        api.toast({
                            msg: res,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }).catch((err) => {
                    api.alert({
                        msg: JSON.stringify(err)
                    });
                })
            },
            //增加数量
            numBox_add: function(cart_id) {

                var $_this = this;
                let shopNum = 0;

                for (let x = 0; x < $_this.shop_list.length; x++) {
                    for (let y = 0; y < $_this.shop_list[x].goods_list.length; y++) {
                        if ($_this.shop_list[x].goods_list[y].cart_id == cart_id) {
                            shopNum = ++$_this.shop_list[x].goods_list[y].goods_nums;
                        }
                    }
                }
                $_this.getAllPice()
                const data = {
                    token: $api.getStorage('token'),
                    cart_id: cart_id,
                    nums: shopNum
                }
                $_this.changeCartGoodsNums(data);

            },
            //跳转
            skip: function() {
                if (this.checkSlide()) {
                    this.restSlide();
                } else {
                    // alert('You click the slide!')
                }
            },
            //滑动开始
            touchStart: function(e) {
                // 记录初始位置
                this.startX = e.touches[0].clientX;
            },
            //滑动结束
            touchEnd: function(e) {
                // 当前滑动的父级元素
                let parentElement = e.currentTarget.parentElement;
                // 记录结束位置
                this.endX = e.changedTouches[0].clientX;
                // 左滑
                if (parentElement.dataset.type == 0 && this.startX - this.endX > 30) {
                    this.restSlide();
                    parentElement.dataset.type = 1;
                }
                // 右滑
                if (parentElement.dataset.type == 1 && this.startX - this.endX < -30) {
                    this.restSlide();
                    parentElement.dataset.type = 0;
                }
                this.startX = 0;
                this.endX = 0;
            },
            //判断当前是否有滑块处于滑动状态
            checkSlide: function() {
                let listItems = document.querySelectorAll('.content_shop_commodity');
                for (let i = 0; i < listItems.length; i++) {
                    if (listItems[i].dataset.type == 1) {
                        return true;
                    }
                }
                return false;
            },
            //复位滑动状态
            restSlide: function() {
                let listItems = document.querySelectorAll('.content_shop_commodity');
                // 复位
                for (let i = 0; i < listItems.length; i++) {
                    listItems[i].dataset.type = 0;
                }
            },
            //删除
            deleteItem: function(cartids) {
                // 当前索引

                var $_this = this;
                const data = {
                    token: $api.getStorage('token'),
                    cartids: cartids
                }
                api.confirm({
                    msg: '确定要删除该商品吗',
                    buttons: ['确定', '取消']
                }, function(ret, err) {
                    var index = ret.buttonIndex;
                    if (index == 1) {
                        Axios.post(window.Url.del_cart_goods_list, data).then(function(res) {
                            api.toast({
                                msg: res.info,
                                duration: 2000,
                                location: 'bottom'
                            });
                            if (res.status == 1) {
                                $_this.getCartList();
                                $_this.restSlide();
                                this.getAllPice();
                            } else {

                            }
                        }).catch((err) => {
                            api.alert({
                                msg: JSON.stringify(err)
                            });
                        })
                    } else {
                        $_this.restSlide();
                    }
                });

            },
            goOrder: function() {
                if (this.checkCommodity.length > 0) {
                    this.cartids = '-' + this.checkCommodity.join('-') + '-';
                    api.openWin({
                        name: 'orderTrue',
                        url: './order_true.html',
                        pageParam: {
                            cartids: this.cartids,
                            type: 3
                        }
                    });
                }
            },
            goMsg: function() {
                api.openWin({
                    name: 'myInfo_my_info',
                    url: '../user/set/myInfo/my_info.html',
                    pageParam: {
                        test: ''
                    }
                });
            },
            //获取总价
            getAllPice: function() {
                var total_money = 0
                var total_num = 0
                for (var ss = 0; ss < this.checkCommodity.length; ss++) {
                    for (var s = 0; s < this.shop_list.length; s++) {
                        for (var i = 0; i < this.shop_list[s].goods_list.length; i++) {
                            if (this.shop_list[s].goods_list[i].cart_id == this.checkCommodity[ss]) {
                                total_num = total_num + Number(this.shop_list[s].goods_list[i].goods_nums)
                                total_money = Number(total_money) + (Number(this.shop_list[s].goods_list[i].goods_price) * Number(this.shop_list[s].goods_list[i].goods_nums))
                            }
                        }
                    }
                }
                this.total_num = total_num
                this.total_money = total_money.toFixed(2);
            }
        }
    })
</script>
